package calao;

import java.util.Vector;

import calao.storage.Record;
import calao.util.Cursor;

public class ProfileManager
extends Service
{
		
	public Profile[] listProfiles()
	{
		Cursor<Record> cursor = ws.storage.list(Profile.TABLE);
		Vector<Profile> result = new Vector<Profile>();
		while (cursor.next()) result.add(new Profile(ws, cursor.get()));
		return result.toArray(new Profile[result.size()]);
	}
	
	public Profile getProfile(int id)
	{
		Record hit = ws.storage.fetch(Profile.TABLE, id);
		return hit!=null? new Profile(ws, hit) : null;
	}
	
	public Profile createProfile(String name, String[] permissions)
	{
		int id = ws.storage.getNextValue(Profile.ID_SEQUENCE);
		storeProfile(id, name, permissions);
		return new Profile(ws, id, name, permissions);
	}
	
	public void storeProfile(int id, String name, String[] permissions)
	{
		Record record = new Record(Profile.TABLE);
		record.set(Profile.ID, id);
		record.set(Profile.NAME, name);
		record.set(Profile.PERMISSIONS, permissions);
		ws.storage.store(record);
	}
	
	public void deleteProfile(int id)
	{
		ws.storage.drop(Profile.TABLE, id);
	}

}
